package com.chinaoly.taian.web;

import com.chinaoly.frm.core.entity.NewRuntimException;
import com.chinaoly.frm.core.entity.Result;
import com.chinaoly.frm.core.entity.ResultCode;
import com.chinaoly.frm.core.entity.ResultGenerator;
import com.chinaoly.baotou.common.exception.BusinessException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 统一异常处理
 *
 * @author xuyh
 * @date 2022/2/24
 */
@Slf4j
@ControllerAdvice
@Order(0)
public class GlobalExceptionHandler {

    @ExceptionHandler(value = MismatchedInputException.class)
    @ResponseBody
    public Result<Void> mismatchedInputException(MismatchedInputException exception) {
        log.info("请求参数结构转换错误：{}", exception.getMessage(), exception);
        return ResultGenerator.genResult(ResultCode.G_J_1001.code, "请求参数结构转换错误", null);
    }

    /**
     * spring valid 参数校验
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<Void> methodArgumentExceptionHandle(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        StringBuilder errorMsg = new StringBuilder();
        List<FieldError> fieldErrors = result.getFieldErrors();
        fieldErrors.forEach(error -> errorMsg.append(error.getDefaultMessage()).append("!"));
        log.info("请求参数错误：{}", errorMsg);
        return ResultGenerator.genResult(ResultCode.G_J_1001.code, errorMsg.toString(), null);
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public Result<Void> bindExceptionHandle(BindException exception) {
        BindingResult result = exception.getBindingResult();
        StringBuilder errorMsg = new StringBuilder();
        List<FieldError> fieldErrors = result.getFieldErrors();
        fieldErrors.forEach(error -> errorMsg.append(error.getDefaultMessage()).append("!"));
        log.info("请求参数错误：{}", errorMsg);
        return ResultGenerator.genResult(ResultCode.G_J_1001.code, errorMsg.toString(), null);
    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result<Void> catchBusinessException(BusinessException e) {
        log.warn("业务异常：", e);
        return ResultGenerator.genResult(ResultCode.G_J_1000.code, e.getMessage(), null);
    }

    @ExceptionHandler(value = NewRuntimException.class)
    @ResponseBody
    public Result<Void> catchNewRuntimException(NewRuntimException e) {
        log.warn("运行时异常：", e);
        int errorCode = e.getErrorCode() == 0 ? ResultCode.FAIL.code : e.getErrorCode();
        return ResultGenerator.genResult(errorCode, e.getMessage(), null);
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<Void> exceptionHandler(Exception exception) {
        log.error("系统异常：", exception);
        return ResultGenerator.genResult(ResultCode.FAIL.code, "系统异常", null);
    }
}
